﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace LightWeb.Infrastructure.DomainModel
{
    [Serializable]
    public abstract class EntityBase : EntityCloneBase
    {

    }

    [Serializable]
    public abstract class EntityCloneBase : IDeepClone, ICloneable
    {
        public int PrimaryKey { get; set; }

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        public object DeepClone()
        {
            MemoryStream memoryStream = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(memoryStream, this);
            memoryStream.Position = 0;
            return formatter.Deserialize(memoryStream);
        }
    }

    public interface IDeepClone
    {
        object DeepClone();
    }
}
